Skip to content

[Feature] add support for a custom CSVFormat #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 23, 2025

Conversation

delei
Copy link
Collaborator

@delei delei commented May 7, 2025

Close #352

What's changed?

Features

  • support delimiters, quotes, and other parameters when reading and writing csv files

Example

Read

FastExcel.read(csvFile, CsvData.class, new CsvDataListener())
    .csv()
    .delimiter(CsvConstant.AT)
    .quote(CsvConstant.DOUBLE_QUOTE,QuoteMode.ALL)
    .recordSeparator(CsvConstant.LF)
    .nullString(CsvConstant.UNICODE_EMPTY)
    .escape(escapse)
    .doReadSync();

Write

FastExcel.write(csvFile, CsvData.class)
    .csv()
    .delimiter(CsvConstant.AT)
    .quote(CsvConstant.DOUBLE_QUOTE,QuoteMode.ALL)
    .recordSeparator(CsvConstant.LF)
    .nullString(CsvConstant.UNICODE_EMPTY)
    .escape(escapse)
    .doWrite(demoData());

Checklist

  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

@delei delei requested review from jipengfei-jpf and psxjoy as code owners May 7, 2025 05:28
delei added 2 commits May 29, 2025 18:20
# Conflicts:
#	fastexcel-core/src/main/java/cn/idev/excel/read/metadata/ReadWorkbook.java
@psxjoy psxjoy requested a review from Copilot May 29, 2025 15:10
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for a custom CSVFormat when reading CSV files by introducing a new csvFormat parameter in the read API and updating the CSV reading behavior accordingly.

  • Added custom CSV test cases with different delimiters and quoting configurations.
  • Extended ReadWorkbook to store a CSVFormat and updated CsvReadWorkbookHolder to use it.
  • Introduced a new csvFormat() method in ExcelReaderBuilder to allow users to specify a custom CSVFormat.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
fastexcel-test/src/test/resources/csv/simple.csv Added simple CSV test file with custom header and numeric data.
fastexcel-test/src/test/resources/csv/simple-quote.csv Added CSV test file for verifying custom quote handling.
fastexcel-test/src/test/resources/csv/simple-delimiter.csv Added CSV test file to test custom delimiter functionality.
fastexcel-test/src/test/java/cn/idev/excel/test/temp/csv/CsvFormatTest.java Created tests for CSVFormat support using both default and custom formats.
fastexcel-core/src/main/java/cn/idev/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java Modified to initialize csvFormat based on a provided value or fallback to CSVFormat.DEFAULT.
fastexcel-core/src/main/java/cn/idev/excel/read/metadata/ReadWorkbook.java Added a new field for CSVFormat with associated documentation.
fastexcel-core/src/main/java/cn/idev/excel/read/builder/ExcelReaderBuilder.java Added a new csvFormat() method to allow chaining of CSVFormat configuration.

@delei delei changed the title [Feature] add support for custom CSVFormat [Feature] add support for a custom CSVFormat May 29, 2025
@delei
Copy link
Collaborator Author

delei commented Jun 19, 2025

hi,@psxjoy

Please don't deal with this PR for now. I think it should be supported to write to the file, I'm working on this task.

@psxjoy psxjoy marked this pull request as draft June 19, 2025 03:42
@alaahong
Copy link
Contributor

Two suggestions here,

  1. pre-defined some common format, here is a reference for common csv format
    https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html
  2. provide configuration injection way to specific the setting besides pre-defined format
    e.g. change default style via properties or yaml, but you should mention the specific value e.g. for splitter char (\t, space, ascii code), BOM, CRLF ...

@delei delei marked this pull request as ready for review June 22, 2025 07:32
@delei
Copy link
Collaborator Author

delei commented Jun 22, 2025

Hi,@psxjoy
ready for review

@delei
Copy link
Collaborator Author

delei commented Jun 22, 2025

Set CSVformat

As #352 mentioned issue easyexcel-issue-3868.
It's still compatible with setting CSVFormat directly.
IMO,This is an unrecommended use way.

Write

CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter(CsvConstant.AT).build();
csvFile = TestFileUtil.createNewFile("csv-delimiter.csv");
try (ExcelWriter excelWriter = FastExcel.write(csvFile, CsvData.class).excelType(ExcelTypeEnum.CSV).build()) {
    WriteWorkbookHolder writeWorkbookHolder = excelWriter.writeContext().writeWorkbookHolder();
    Workbook workbook = writeWorkbookHolder.getWorkbook();
    if (workbook instanceof CsvWorkbook) {
        CsvWorkbook csvWorkbook = (CsvWorkbook) workbook;
        csvWorkbook.setCsvFormat(csvFormat);
        writeWorkbookHolder.setWorkbook(csvWorkbook);
    }
    WriteSheet writeSheet = FastExcel.writerSheet(0).build();
    excelWriter.write(csvDataList, writeSheet);
}

Read

CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter(CsvConstant.AT).build();
csvFile = TestFileUtil.readFile("csv-delimiter.csv");
try (ExcelReader excelReader = FastExcel.read(csvFile, CsvData.class, new CsvDataListener()).build()) {
    ReadWorkbookHolder readWorkbookHolder = excelReader.analysisContext().readWorkbookHolder();
    if (readWorkbookHolder instanceof CsvReadWorkbookHolder) {
        CsvReadWorkbookHolder csvReadWorkbookHolder = (CsvReadWorkbookHolder) readWorkbookHolder;
        csvReadWorkbookHolder.setCsvFormat(csvFormat);
    }
    ReadSheet readSheet = FastExcel.readSheet(0).build();
    excelReader.read(readSheet);
}

Copy link
Member

@psxjoy psxjoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good PR, I think it complements the CSV format very well.

@psxjoy psxjoy merged commit b4d9427 into fast-excel:main Jun 23, 2025
4 checks passed
@psxjoy psxjoy added this to the 1.3.0 milestone Jun 23, 2025
@delei delei deleted the feature/custom-CSVFormat branch June 23, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] add support for custom CSVFormat
3 participants